package excel;

import java.io.*;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.List;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.excel.EasyExcel;
import org.junit.Test;

public class ExcelTest {

  private static final MathContext TO_10_SF = new MathContext(10, RoundingMode.HALF_UP);

  @Test
  public void test2(){
    double val = 1000.123456789;
    BigDecimal bigDecimal1 = new BigDecimal(val);
    System.out.println(bigDecimal1);
    BigDecimal bigDecimal2 = BigDecimal.valueOf(val);
    System.out.println(bigDecimal2);
    final double rounded = new BigDecimal(val).round(TO_10_SF).doubleValue();
    System.out.println(rounded);
  }

  /**
   * 小数点丢失问题
   */
  @Test
  public void test1() {
    File file = new File("/Users/ycy/Desktop/C4-小区村管道燃气情况统计表.xlsx");
    FileInputStream in = null;

    List<RaPlStatForTownUploadExcel> raPlStatForTownUploadExcels = EasyExcel.read(file)
            .head(RaPlStatForTownUploadExcel.class)
            .sheet(0).headRowNumber(2)
            .doReadSync();

    for (RaPlStatForTownUploadExcel raPlStatForTownUploadExcel : raPlStatForTownUploadExcels) {
      System.out.println(raPlStatForTownUploadExcel);
      BigDecimal plRaLength = BigDecimal.valueOf(Double.parseDouble(raPlStatForTownUploadExcel.getPlRaLength()));
      BigDecimal plAuxLength = BigDecimal.valueOf(Double.parseDouble(raPlStatForTownUploadExcel.getPlRaLength()));
      System.out.println(plRaLength);
      System.out.println(plAuxLength);
    }

    try {
      in = new FileInputStream(file);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
    ExcelReader excelReader = ExcelUtil.getReader(in);
    List<List<Object>> excelData;
    excelData = excelReader.read(0, excelReader.getRowCount());
    for (List<Object> excelDatum : excelData) {
      for (Object o : excelDatum) {
//        System.out.println(o);
      }
    }
  }

}
